package com.boboPlanet.user.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.boboPlanet.user.filter.LogInterceptor;
import com.boboPlanet.user.filter.PlanetMiddleRequestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.http.MediaType;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO fastjson对返回值处理
 * @Author: mis_wu
 * @Date: 2023/2/28
 **/
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Resource
    private PlanetMiddleRequestInterceptor planetMiddleRequestInterceptor;

    @Resource
    private LogInterceptor logInterceptor;

    /**
     * 注册自定义拦截器
     * @param registry InterceptorRegistry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(planetMiddleRequestInterceptor).addPathPatterns("/**");
//                .excludePathPatterns(ConstanceEnum.swagger_url);
        registry.addInterceptor(logInterceptor);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        //Long类型转String类型
        SerializeConfig serializeConfig = SerializeConfig.globalInstance;
        // ToStringSerializer 是这个包 com.alibaba.fastjson.serializer.ToStringSerializer
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        config.setSerializeConfig(serializeConfig);
        config.setSerializerFeatures(
            //保留map空的字段
            SerializerFeature.WriteMapNullValue,
            //将String类型转换成""
            SerializerFeature.WriteNullStringAsEmpty,
            //将Number类型转换成0
            SerializerFeature.WriteNullNumberAsZero,
            //将List的null转换为[]
            SerializerFeature.WriteNullListAsEmpty,
            //将Boolean类型的null转换为false
            SerializerFeature.WriteNullBooleanAsFalse,
            //避免循环引用
            SerializerFeature.DisableCircularReferenceDetect
        );
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        ArrayList<MediaType> mediaTypeList = new ArrayList<>();
        //解决中文乱码问题,相当于controller上的@RequestMapping中加了个属性produces="application/json"
        mediaTypeList.add(MediaType.APPLICATION_JSON);
        converter.setSupportedMediaTypes(mediaTypeList);
        converters.add(converter);
    }

}
